package com.hdfs;

import com.alibaba.fastjson.JSON;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileDirectory {

    //遍历获取 HDFS 文件绝对路径
    public static List<Path> getFilesUnderFolder(FileSystem fs, Path folderPath, String pattern) throws IOException {
        List<Path> paths = new ArrayList<Path>();
        if (fs.exists(folderPath)) {
            FileStatus[] fileStatus = fs.listStatus(folderPath);
            for (int i = 0; i < fileStatus.length; i++) {
                FileStatus fileStatu = fileStatus[i];
                if (!fileStatu.isDirectory()) {//只要文件
                    Path oneFilePath = fileStatu.getPath();
                    if (pattern == null) {
                        paths.add(oneFilePath);
                    } else {
                        if (oneFilePath.getName().contains(pattern)) {
                            paths.add(oneFilePath);
                        }
                    }
                }
            }
        }
        return paths;
    }
    public static List<String> getList() throws IOException {
        Configuration conf = new Configuration();
        conf.set("dfs.blocksize", "128m");
        conf.set("dfs.replication", "2");
        conf.set("fs.defaultFS", "hdfs://node-1:9000");
        Path path = new Path("/data/log");

        FileSystem fs = FileSystem.newInstance(conf);
        //根据正则表达式来过滤文件名
        List<String> directory = new ArrayList<>();
        List<Path> filesUnderFolder = FileDirectory.getFilesUnderFolder(fs, path, ".");
        for (Path i : filesUnderFolder) {
            //得到视频格式
            if (i.toString().endsWith(".mp4")) {
                String finalPath= i.toString().substring(18,i.toString().length());

                directory.add(finalPath);
            }
        }
        return directory;
    }

    public static void main(String[] args) throws IOException {
        List<String> list = FileDirectory.getList();

        String jsonStr = JSON.toJSONString(list);
        System.out.println(jsonStr);
    }
}
